import requests
import bs4
import os
import time

urlHome = 'http://codeforces.com/'
urlEnter = 'http://codeforces.com/enter/'
urlSubmit = 'http://codeforces.com/problemset/submit'

def initSession():
	return requests.session()

def login(session, user_id, passwd):
	response = session.get(urlEnter)
	page = response.content
	bs = bs4.BeautifulSoup(page)
	meta = bs.html.head.find(name='meta', attrs={'name':'X-Csrf-Token'})
	csrfToken = meta['content']
	print csrfToken
	loginData = { 'csrf_token':csrfToken, 'action':'enter', 
					 'handle':user_id, 'password':passwd, '_tta':'940' }
	session.post(urlEnter, loginData)
	return csrfToken

def submit(session, csrfToken, user_id, passwd):
	submitData = { 'csrf_token':csrfToken, 
		'action':'submitSolutionFormSubmittedv', 'contestId':'483', 
		'submittedProblemIndex':'A', 'programTypeId':'1', 
		'source':time.ctime()+'\nhello\n' } 
	response = session.post(urlSubmit, submitData)
	print response.url
	print 'end'

def main():
	session = initSession()
	csrfToken = login(session, 'grated', 'bfezgl516')
	submit(session, csrfToken, 'grated', 'bfezgl516')

if __name__ == '__main__':
	main()
